1301D - Time to Run - CodeForces Solution


constructive algorithms graphs implementation *2000

Please click on ads to support us..

C++ Code:

/*#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>

using namespace std;

//change the long long to int if you need to save memory/time really badly
typedef long long ll;
typedef long double ld;

const ll MAXN = 2e5 + 5;
const ll INF = 1e14;
const ll MOD = 1e9 + 7;

ll __gcd(ll a, ll b) {
    if (b == 0) {
        return a;
    }
    
    if (a == 0) {
        return b;
    }
    
    if (a < b) {
        swap(a, b);
    }
    
    return __gcd(b, a % b);
}

inline ll dig(char c) {
    ll ret = c - 'A';
    return ret;
}
 
inline ll compute(ll ind) {
    ll ret = 1;
    for (ll j = 1; j <= ind; j++) {
        ret *= 10;
    }
    return ret;
}

vector<pair<ll, char>> ans;
bool check(ll val) {
    if (val == 0) {
        cout << "YES" << endl;
        cout << ans.size() << endl;
        for (auto el: ans) {
            cout << el.first << " " << el.second << endl;
        }
        return true;
    }
    return false;
}
 
void solve(ll ca)
{
    ll n, m, k;
    cin >> n >> m >> k;
    
    //handle edge cases where one or both dimensions are equal to 1
    
    
    ll totalnum = 4*n*m - 2*n - 2*m;
    if (k > totalnum) {
        cout << "NO" << endl;
        return;
    }
    
    if (n == 1) {
        ll tamt = min(m-1, k);
        ans.push_back({tamt, 'R'});
        k -= tamt;
        if (check(k)) {
            return;
        }
        tamt = min(m-1, k);
        ans.push_back({tamt, 'L'});
        k -= tamt;
        if (check(k)) {
            return;
        }
    } else if (m == 1) {
        ll tamt = min(n-1, k);
        ans.push_back({tamt, 'D'});
        k -= tamt;
        if (check(k)) {
            return;
        }
        tamt = min(n-1, k);
        ans.push_back({tamt, 'U'});
        k -= tamt;
        if (check(k)) {
            return;
        }
    }
        
    ll col = 0;
    while (k > 0 && col < m-1) {
        //first go down as much as possible
        ll amt = min(k, n-1);
        ans.push_back({amt, 'D'});
        k -= amt;
        
        if (check(k)) {
            return;
        }
        
        amt = min(k, n-1);
        ans.push_back({amt, 'U'});
        k -= amt;
        
        if (check(k)) {
            return;
        }
        
        ans.push_back({1, 'R'});
        k--;
        
        if (check(k)) {
            return;
        }
        
        col++;
    }
    
    if (check(k)) {
        return;
    }
    
    ans.push_back({1, 'D'});
    k--;
    
    if (check(k)) {
        return;
    }
    
    ll row = 1;
    while (k > 0 && row < n) {
        //first go down as much as possible
        ll amt = min(k, m-1);
        ans.push_back({amt, 'L'});
        k -= amt;
        
        if (check(k)) {
            return;
        }
        
        amt = min(k, m-1);
        ans.push_back({amt, 'R'});
        k -= amt;
        
        if (check(k)) {
            return;
        }
        
        if (row < n-1) {
            ans.push_back({1, 'D'});
            k--;
            
            if (check(k)) {
                return;
            }
        }
        
        row++;
    }
    
    ll amt = min(k, n-1);
    ans.push_back({amt, 'U'});
    k -= amt;
    
    if (check(k)) {
        return;
    }
    
    amt = min(k, m-1);
    ans.push_back({amt, 'L'});
    k -= amt;
    
    if (check(k)) {
        return;
    }
    
    return;
}

int main()
{
    //mt19937 rng(0);
    
    //Fast IO
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    
    /*
     freopen("sabotage.in", "r", stdin);
     freopen("sabotage.out", "w", stdout);
     */
    
    ll t = 1;
    //cin >> t;
    
    ll co = 1;
    while (t--) {
        solve(co);
        ++co;
    }
}


Comments

Submit
0 Comments
More Questions

1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves